package newfeature;

import org.junit.Test;

import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author liweimin
 * @date 2021/4/6 18:24
 * @description java8新特性
 * @since
 */
public class Java8Test {
    public static String toUpperString(MyFun<String> myFun, String s) {
//        return null;
        return myFun.getValue(s);
    }


    @Test
    public void testLambda() {
        //Lambda表达式
        Runnable runnable = () -> System.out.println("Hello World");

        //匿名类
        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World");
            }
        };

        System.out.println(toUpperString(s -> s.toUpperCase(), "asd"));

        //lambda表达式
        TreeSet<String> treeSet = new TreeSet<>(((o1, o2) -> Integer.compare(o1.length(), o2.length())));
        TreeSet<String> treeSet1 = new TreeSet<>((Comparator.comparingInt(String::length)));

        //匿名类
        TreeSet<String> treeSet2 = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(), o2.length());
            }
        });
    }

    @Test
    public void testMethodRefer() {
        Consumer<String> consumer = x -> System.out.println(x);
        Consumer<String> consumer1 = System.out::println;
        consumer.accept("qw");

        Comparator<Integer> comparator = (x1, x2) -> Integer.compare(x1, x2);
        Comparator<Integer> comparator1 = Integer::compareTo;

        BiPredicate<String, String> bp = (x, y) -> x.equals(y);
        BiPredicate<String, String> bp1 = String::equals;

    }

    @Test
    public void testConstructorRefer() {
        Function<Integer, Person> function = Person::new;
        Person person = function.apply(12);
        System.out.println(person);
        Function<Integer, Integer[]> function1 = Integer[]::new;
        Integer[] apply = function1.apply(12);
        System.out.println("长度为：" + apply.length);
    }

    @Test
    public void test() {
        Function<Integer, Integer[]> function = integer -> {
            Integer[] integers = new Integer[integer];
            for (int i = 0; i < integers.length; i++) {
                integers[i] = integer;
            }
            return integers;
        };
        Function<Integer[], Integer> function1 = (integers) -> {
            int result = 0;
            for (int i = 0; i < integers.length; i++) {
                result += integers[i];
            }
            return result;
        };
        Function<Integer, Integer> integerIntegerFunction = function.andThen(function1);
        System.out.println("数组相加求和：" + integerIntegerFunction.apply(12));
    }

    @Test
    public void test1() {
        List list = new ArrayList();
        list.stream();
//        Arrays.stream();
//        Stream.of()
//
//        Stream.generate();
//        Stream.iterate()
        Stream<Integer> stream = Stream.iterate(0, x -> x + 2);
        stream.limit(100).forEach(System.out::println);

        Stream<Double> stream1 = Stream.generate(Math::random);
        stream1.limit(100).forEach(System.out::println);
    }

    @Test
    public void testOptional1() {
        Person person = new Person(12);
        Optional<Person> opt = Optional.ofNullable(person);
        opt.ifPresent(System.out::println);
    }

    @Test
    public void testOptional2() {

        Optional<Person> opt = Optional.ofNullable(null);
        Person person = opt.orElse(new Person(12));
    }
}

@FunctionalInterface
interface MyNumber {
    public double getValue();

}

@FunctionalInterface
interface MyFun<T> {
    public T getValue(T t);
}

interface parent {
    default void show() {
        System.out.println("123");
    }
}

interface son {
    default void show() {
        System.out.println("456");
    }

    default String cout(String s) {
        return s.toUpperCase();
    }
}

interface pa {
    String cout(String s);
}

class sonson implements parent, son, pa {


    @Override
    public void show() {
        parent.super.show();
    }

    @Override
    public String cout(String s) {
        return son.super.cout(s);
    }
}